package cn.security.auth.service.impl;

import cn.security.auth.common.Result;
import cn.security.auth.entity.LoginParam;
import cn.security.auth.service.LoginService;
import cn.security.auth.util.JwtUtil;
import cn.security.auth.util.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService{

    @Value("${custom.security.default.account}")
    private String defaultAccount;

    @Value("${custom.security.default.password}")
    private String defaultPassword;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    @Qualifier("customAuthenticationManagerConfig")
    private AuthenticationManager authenticationManager;
    @Override
    public Result match(LoginParam param) throws Exception {
        // 拓展:此处需要前端传递的密码进行解密后传递【明文】
        //UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(param.getUsername(), param.getPassword());
        //// todo:AuthenticationManager authenticate()进行用户认证
        //Authentication authenticate = authenticationManager.authenticate(authentication);
        // todo:如果认证通过，使用JwtUtil生成Token
        //String token = JwtUtil.generateToken(param.getUsername(), 5);
        //WebUtils.getResponse().setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
        if(defaultAccount.equals(param.getUsername()) && passwordEncoder.matches(param.getPassword(), passwordEncoder.encode(defaultPassword))){
            return Result.success("验证成功");
        }
        // todo:把完整信息存如redis
        //SecurityContextHolder.getContext().setAuthentication(authenticate);
        return Result.error("登录失败");
    }


    @Override
    public Result loginAndSetHeader(LoginParam param) throws Exception {
        // 拓展:此处需要前端传递的密码进行解密后传递【明文】
        //// todo:AuthenticationManager authenticate()进行用户认证
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(param.getUsername(), param.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authentication);
        if (authenticate != null && authenticate.isAuthenticated()) {
            // todo:如果认证通过，使用JwtUtil生成Token
            String token = JwtUtil.generateToken(param.getUsername(), 5);
            WebUtils.getResponse().setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
            return Result.success("登录成功");
        }
        // todo:把完整信息存如redis
        //SecurityContextHolder.getContext().setAuthentication(authenticate);
        return Result.error("登录失败");
    }
    @Override
    public User getDefaultLoginUser() {
      return  new User(defaultAccount, passwordEncoder.encode(defaultPassword), true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_USER"));
    }
}
